# TODO: Add comment
# 
# Author: yaping
###############################################################################
##parameters: working_dir, file_one, file_two, NDR_p_value_threshold, annotation_file, overlap_or_not_with_annotation_location, output_bed_file_in_different_combination_with_gene_names
##each column's content:  chr	start	end	aveMethyWind	gchNumWind	gchDepthWind	gchCTdepthWind	gchDotWind	sigValue	aveMethyWindLinker	wcgMethyWind	wcgNumWind


for (e in commandArgs(TRUE)) {
	ta = strsplit(e,"=",fixed=TRUE)
	if(! is.na(ta[[1]][2])) {
		if(ta[[1]][1] == "wd"){
			wd<-ta[[1]][2]  ## directory to make plot file
			
		}
		if(ta[[1]][1] == "file1"){
			file1<-ta[[1]][2]
		}
		if(ta[[1]][1] == "sample1"){
			sample1<-ta[[1]][2]
		}
		if(ta[[1]][1] == "file2"){
			file2<-ta[[1]][2]
		}
		if(ta[[1]][1] == "sample2"){
			sample2<-ta[[1]][2]
		}
		if(ta[[1]][1] == "file3"){
			file3<-ta[[1]][2]
		}
		if(ta[[1]][1] == "sample3"){
			sample3<-ta[[1]][2]
		}
		if(ta[[1]][1] == "pValue"){
			pValue<-ta[[1]][2]
		}
		if(ta[[1]][1] == "anno"){
			anno<-ta[[1]][2]
		}
		if(ta[[1]][1] == "annoFormat"){
			annoFormat<-ta[[1]][2]
		}
		if(ta[[1]][1] == "overlap"){
			overlap<-as.numeric(ta[[1]][2])  ##0: no overlap; 1: overlap
		}
		if(ta[[1]][1] == "outputBed"){
			outputBed<-as.numeric(ta[[1]][2])  ## 1: output; others: no
		}
		if(ta[[1]][1] == "useWcg"){
			useWcg<-as.numeric(ta[[1]][2])  ## 1: use cg methylation value; others: no
		}
		if(ta[[1]][1] == "dataPoint"){
			dataPoint<-as.numeric(ta[[1]][2])  ## 1: use cg methylation value; others: no
		}
		
	}
}
setwd(wd)
library(Ringo)

Obj1<-read.table(file1,sep="\t",header=T)
Obj2<-read.table(file2,sep="\t",header=T)
Obj3_tmp<-read.table(file3,sep="\t",header=T)
Obj4_tmp<-read.table(file4,sep="\t",header=T)

Obj3<-cbind(Obj3_tmp[,1:3],Obj3_tmp[,4],Obj3_tmp[,9],Obj4_tmp[,4],Obj4_tmp[,9],Obj3_tmp[,8],Obj4_tmp[,8])

Obj1<-Obj1[!is.na(Obj1[,9]) & !is.na(Obj1[,4]) & Obj1[,8] > dataPoint,]
Obj2<-Obj2[!is.na(Obj2[,9]) & !is.na(Obj2[,4]) & Obj2[,8] > dataPoint,]
Obj3<-Obj3[!is.na(Obj3[,4]) & !is.na(Obj3[,5]) & !is.na(Obj3[,6]) & !is.na(Obj3[,7]) & Obj3[,8] > dataPoint & Obj3[,9] > dataPoint,]



Obj1 <-cbind(Obj1, p.adjust(Obj1[,9],method="BH"))
Obj2 <-cbind(Obj2, p.adjust(Obj2[,9],method="BH"))
Obj3<-Obj3[,1:3]



Obj1Data<-array()
Obj2Data<-array()
Obj3Data<-Obj3

for(i in c(1:22)){
	chrName=paste("chr",i,sep="")
	tmpObj1<-Obj1[Obj1[,1] %in% chrName,]
	tmpObj2<-Obj2[Obj2[,1] %in% chrName,]
	tmpObj3<-Obj3[Obj3[,1] %in% chrName,]
	
	Obj1LocData<-data.frame(chr= tmpObj1[,1], start= tmpObj1[,2], end= tmpObj1[,3])
	Obj2LocData<-data.frame(chr= tmpObj2[,1], start= tmpObj2[,2], end= tmpObj2[,3])
	Obj3LocData<-data.frame(chr= tmpObj3[,1], start= tmpObj3[,2], end= tmpObj3[,3])
	
	Obj1Obj2tssOverlapped<-as.matrix(regionOverlap(Obj1LocData, Obj2LocData))


	
	
	Obj1Data<-rbind(Obj1Data,cbind(tmpObj1, rowSums(Obj1Obj2tssOverlapped)))
	Obj2Data<-rbind(Obj2Data,cbind(tmpObj2, rowSums(t(Obj1Obj2tssOverlapped))))

	
}
Obj1Data<-Obj1Data[2:length(Obj1Data[,1]),]
Obj2Data<-Obj2Data[2:length(Obj2Data[,1]),]




Obj1NDRInObj1<-Obj1Data[Obj1Data[,13]< pValue,]
Obj2NDRInObj2<-Obj2Data[Obj2Data[,13]< pValue,]


Obj1NDR<-Obj1Data[Obj1Data[,14]!=0 & Obj1Data[,13]< pValue,]
Obj2NDR<-Obj2Data[Obj2Data[,14]!=0 & Obj2Data[,13]< pValue,]
Obj3NDR<-Obj3Data

sharedWindow<-Obj3Data[Obj1Data[,14]!=0,]

Obj1LocNDR<-data.frame(chr= Obj1NDR$chr, start= Obj1NDR$start, end= Obj1NDR$end)
Obj2LocNDR<-data.frame(chr= Obj2NDR$chr, start= Obj2NDR$start, end= Obj2NDR$end)
Obj3LocNDR<-data.frame(chr= Obj3NDR[,1], start= Obj3NDR[,2], end= Obj3NDR[,3])

Obj1Obj2NDRoverlapped<-as.matrix(regionOverlap(Obj1LocNDR, Obj2LocNDR))
NumObj1Obj2NDRoverlapped<-dim(Obj1Obj2NDRoverlapped[rowSums(Obj1Obj2NDRoverlapped)!=0,])

Obj1Obj3NDRoverlapped<-as.matrix(regionOverlap(Obj1LocNDR, Obj3LocNDR))
NumObj1Obj3NDRoverlapped<-dim(Obj1Obj3NDRoverlapped[rowSums(Obj1Obj3NDRoverlapped)!=0,])

Obj2Obj3NDRoverlapped<-as.matrix(regionOverlap(Obj2LocNDR, Obj3LocNDR))
NumObj2Obj3NDRoverlapped<-dim(Obj2Obj3NDRoverlapped[rowSums(Obj2Obj3NDRoverlapped)!=0,])

overlapped3<-cbind(rowSums(Obj1Obj2NDRoverlapped),rowSums(Obj1Obj3NDRoverlapped))
NumNDRoverlapped3 <- dim(overlapped3[overlapped3[,1]!=0 & overlapped3[,2]!=0,])

if(!is.null(anno)){
	
	annoFile<-read.table(anno,sep="\t",header=F)
	annoLoc<-data.frame(chr= annoFile[,1], start= annoFile[,2], end=annoFile[,3])
	if(annoFormat == "gtf"){
		annoLoc<-data.frame(chr= annoFile[,1], start= annoFile[,4], end=annoFile[,5])
	}
	callableWindowObj1Data=Obj1Data
	callableWindowObj2Data=Obj2Data
	callableWindowObj3Data=Obj3Data
	callableWindowLocObj1<-data.frame(chr= callableWindowObj1Data[,1], start= callableWindowObj1Data[,2], end=callableWindowObj1Data[,3])
	callableWindowLocObj2<-data.frame(chr= callableWindowObj2Data[,1], start= callableWindowObj2Data[,2], end=callableWindowObj2Data[,3])
	callableWindowLocObj3<-data.frame(chr= callableWindowObj3Data[,1], start= callableWindowObj3Data[,2], end=callableWindowObj3Data[,3])
	
	Obj1DataAnnoInfo<-array()
	Obj2DataAnnoInfo<-array()
	Obj3DataAnnoInfo<-array()
	
	for(i in c(1:22)){
		chrName=paste("chr",i,sep="")
		tmpObj1<-callableWindowObj1Data[callableWindowObj1Data[,1] %in% chrName,]
		tmpObj2<-callableWindowObj2Data[callableWindowObj2Data[,1] %in% chrName,]
		tmpObj3<-callableWindowObj3Data[callableWindowObj3Data[,1] %in% chrName,]
		
		tmpAnno<-annoFile[annoFile[,1] %in% chrName,]
		
		Obj1LocData<-data.frame(chr= tmpObj1[,1], start= tmpObj1[,2], end= tmpObj1[,3])
		Obj2LocData<-data.frame(chr= tmpObj2[,1], start= tmpObj2[,2], end= tmpObj2[,3])
		Obj3LocData<-data.frame(chr= tmpObj3[,1], start= tmpObj3[,2], end= tmpObj3[,3])
		
		annoLocTmp<-data.frame(chr= tmpAnno[,1], start= tmpAnno[,2], end= tmpAnno[,3])
		
		if(annoFormat == "gtf"){
			annoLocTmp<-data.frame(chr= tmpAnno[,1], start= tmpAnno[,4], end=tmpAnno[,5])
		}
		
		Obj1AnnoOverlapped<-as.matrix(regionOverlap(Obj1LocData, annoLocTmp))
		Obj2AnnoOverlapped<-as.matrix(regionOverlap(Obj2LocData, annoLocTmp))
		Obj3AnnoOverlapped<-as.matrix(regionOverlap(Obj3LocData, annoLocTmp))
		
		Obj1DataAnnoInfo<-rbind(Obj1DataAnnoInfo,cbind(tmpObj1, rowSums(Obj1AnnoOverlapped)))
		Obj2DataAnnoInfo<-rbind(Obj2DataAnnoInfo,cbind(tmpObj2, rowSums(Obj2AnnoOverlapped)))
		Obj3DataAnnoInfo<-rbind(Obj3DataAnnoInfo,cbind(tmpObj3, rowSums(Obj3AnnoOverlapped)))
		
	}
	Obj1DataAnnoInfo<-Obj1DataAnnoInfo[2:length(Obj1DataAnnoInfo[,1]),]
	Obj2DataAnnoInfo<-Obj2DataAnnoInfo[2:length(Obj2DataAnnoInfo[,1]),]
	Obj3DataAnnoInfo<-Obj3DataAnnoInfo[2:length(Obj3DataAnnoInfo[,1]),]
	
	Obj1DataAnnoInfo<-Obj1DataAnnoInfo[!is.na(Obj1DataAnnoInfo[,1]),]
	Obj2DataAnnoInfo<-Obj2DataAnnoInfo[!is.na(Obj2DataAnnoInfo[,1]),]
	Obj3DataAnnoInfo<-Obj3DataAnnoInfo[!is.na(Obj3DataAnnoInfo[,1]),]
	
	Obj1Anno<-Obj1DataAnnoInfo[Obj1DataAnnoInfo[,16]!=0,]
	Obj2Anno<-Obj2DataAnnoInfo[Obj2DataAnnoInfo[,16]!=0,]
	Obj3Anno<-Obj3DataAnnoInfo[Obj3DataAnnoInfo[,16]!=0,]
	
	Obj1NDRannoInObj1<-Obj1DataAnnoInfo[Obj1DataAnnoInfo[,16]!=0 & Obj1DataAnnoInfo[,13]<pValue,]
	Obj2NDRannoInObj2<-Obj2DataAnnoInfo[Obj2DataAnnoInfo[,16]!=0 & Obj2DataAnnoInfo[,13]<pValue,]
	Obj3NDRannoInObj3<-Obj3DataAnnoInfo[Obj3DataAnnoInfo[,16]!=0 & Obj3DataAnnoInfo[,13]<pValue,]
	
	Obj1NDRanno<-Obj1DataAnnoInfo[Obj1DataAnnoInfo[,14]!=0 & Obj1DataAnnoInfo[,15]!=0 & Obj1DataAnnoInfo[,16]!=0 & Obj1DataAnnoInfo[,13]<pValue,]
	Obj2NDRanno<-Obj2DataAnnoInfo[Obj2DataAnnoInfo[,14]!=0 & Obj2DataAnnoInfo[,15]!=0 & Obj2DataAnnoInfo[,16]!=0 & Obj2DataAnnoInfo[,13]<pValue,]
	Obj3NDRanno<-Obj3DataAnnoInfo[Obj3DataAnnoInfo[,14]!=0 & Obj3DataAnnoInfo[,15]!=0 & Obj3DataAnnoInfo[,16]!=0 & Obj3DataAnnoInfo[,13]<pValue,]
	
	if(useWcg){
		Obj1Anno<-Obj1DataAnnoInfo[Obj1DataAnnoInfo[,16]!=0 & (Obj1DataAnnoInfo[,11]<0.2 | is.na(Obj1DataAnnoInfo[,11])),]
		Obj2Anno<-Obj2DataAnnoInfo[Obj2DataAnnoInfo[,16]!=0 & (Obj2DataAnnoInfo[,11]<0.2 | is.na(Obj2DataAnnoInfo[,11])),]
		Obj3Anno<-Obj3DataAnnoInfo[Obj3DataAnnoInfo[,16]!=0 & (Obj3DataAnnoInfo[,11]<0.2 | is.na(Obj3DataAnnoInfo[,11])),]
		
		Obj1NDRannoInObj1<-Obj1DataAnnoInfo[Obj1DataAnnoInfo[,16]!=0 & Obj1DataAnnoInfo[,13]<pValue & (Obj1DataAnnoInfo[,11]<0.2 | is.na(Obj1DataAnnoInfo[,11])),]
		Obj2NDRannoInObj2<-Obj2DataAnnoInfo[Obj2DataAnnoInfo[,16]!=0 & Obj2DataAnnoInfo[,13]<pValue & (Obj2DataAnnoInfo[,11]<0.2 | is.na(Obj2DataAnnoInfo[,11])),]
		Obj3NDRannoInObj3<-Obj3DataAnnoInfo[Obj3DataAnnoInfo[,16]!=0 & Obj3DataAnnoInfo[,13]<pValue & (Obj3DataAnnoInfo[,11]<0.2 | is.na(Obj3DataAnnoInfo[,11])),]
		
		Obj1NDRanno<-Obj1DataAnnoInfo[Obj1DataAnnoInfo[,14]!=0 & Obj1DataAnnoInfo[,15]!=0 & Obj1DataAnnoInfo[,16]!=0 & Obj1DataAnnoInfo[,13]<pValue & (Obj1DataAnnoInfo[,11]<0.2 | is.na(Obj1DataAnnoInfo[,11])),]
		Obj2NDRanno<-Obj2DataAnnoInfo[Obj2DataAnnoInfo[,14]!=0 & Obj2DataAnnoInfo[,15]!=0 & Obj2DataAnnoInfo[,16]!=0 & Obj2DataAnnoInfo[,13]<pValue & (Obj2DataAnnoInfo[,11]<0.2 | is.na(Obj2DataAnnoInfo[,11])),]
		Obj3NDRanno<-Obj3DataAnnoInfo[Obj3DataAnnoInfo[,14]!=0 & Obj3DataAnnoInfo[,15]!=0 & Obj3DataAnnoInfo[,16]!=0 & Obj3DataAnnoInfo[,13]<pValue & (Obj3DataAnnoInfo[,11]<0.2 | is.na(Obj3DataAnnoInfo[,11])),]
		
	}
	
	sharedWindAnno<-Obj1DataAnnoInfo[Obj1DataAnnoInfo[,14]!=0 & Obj1DataAnnoInfo[,15]!=0 & Obj1DataAnnoInfo[,16]!=0,]
	if(overlap==0){
		Obj1NDRanno<-Obj1DataAnnoInfo[Obj1DataAnnoInfo[,14]!=0 & Obj1DataAnnoInfo[,15]!=0 & Obj1DataAnnoInfo[,16]==0 & Obj1DataAnnoInfo[,13]<pValue,]
		Obj2NDRanno<-Obj2DataAnnoInfo[Obj2DataAnnoInfo[,14]!=0 & Obj2DataAnnoInfo[,15]!=0 & Obj2DataAnnoInfo[,16]==0 & Obj2DataAnnoInfo[,13]<pValue,]
		Obj3NDRanno<-Obj3DataAnnoInfo[Obj3DataAnnoInfo[,14]!=0 & Obj3DataAnnoInfo[,15]!=0 & Obj3DataAnnoInfo[,16]==0 & Obj3DataAnnoInfo[,13]<pValue,]	
		sharedWindAnno<-Obj1DataAnnoInfo[Obj1DataAnnoInfo[,14]!=0 & Obj1DataAnnoInfo[,15]!=0 & Obj1DataAnnoInfo[,16]==0,]
		Obj1Anno<-Obj1DataAnnoInfo[Obj1DataAnnoInfo[,16]==0,]
		Obj2Anno<-Obj2DataAnnoInfo[Obj2DataAnnoInfo[,16]==0,]
		Obj3Anno<-Obj3DataAnnoInfo[Obj3DataAnnoInfo[,16]==0,]
		Obj1NDRannoInObj1<-Obj1DataAnnoInfo[Obj1DataAnnoInfo[,16]==0 & Obj1DataAnnoInfo[,13]<pValue,]
		Obj2NDRannoInObj2<-Obj2DataAnnoInfo[Obj2DataAnnoInfo[,16]==0 & Obj2DataAnnoInfo[,13]<pValue,]
		Obj3NDRannoInObj3<-Obj3DataAnnoInfo[Obj3DataAnnoInfo[,16]==0 & Obj3DataAnnoInfo[,13]<pValue,]
		sharedWindAnno<-Obj1DataAnnoInfo[Obj1DataAnnoInfo[,14]!=0 & Obj1DataAnnoInfo[,15]!=0 & Obj1DataAnnoInfo[,16]==0,]
		if(useWcg){
			Obj1NDRanno<-Obj1DataAnnoInfo[Obj1DataAnnoInfo[,14]!=0 & Obj1DataAnnoInfo[,15]!=0 & Obj1DataAnnoInfo[,16]==0 & Obj1DataAnnoInfo[,13]<pValue & (Obj1DataAnnoInfo[,11]>=0.2 | is.na(Obj1DataAnnoInfo[,11])),]
			Obj2NDRanno<-Obj2DataAnnoInfo[Obj2DataAnnoInfo[,14]!=0 & Obj2DataAnnoInfo[,15]!=0 & Obj2DataAnnoInfo[,16]==0 & Obj2DataAnnoInfo[,13]<pValue & (Obj2DataAnnoInfo[,11]>=0.2 | is.na(Obj2DataAnnoInfo[,11])),]
			Obj3NDRanno<-Obj3DataAnnoInfo[Obj3DataAnnoInfo[,14]!=0 & Obj3DataAnnoInfo[,15]!=0 & Obj3DataAnnoInfo[,16]==0 & Obj3DataAnnoInfo[,13]<pValue & (Obj3DataAnnoInfo[,11]>=0.2 | is.na(Obj3DataAnnoInfo[,11])),]	
			
			Obj1Anno<-Obj1DataAnnoInfo[Obj1DataAnnoInfo[,16]==0 & (Obj1DataAnnoInfo[,11]>=0.2 | is.na(Obj1DataAnnoInfo[,11])),]
			Obj2Anno<-Obj2DataAnnoInfo[Obj2DataAnnoInfo[,16]==0 & (Obj2DataAnnoInfo[,11]>=0.2 | is.na(Obj2DataAnnoInfo[,11])),]
			Obj3Anno<-Obj3DataAnnoInfo[Obj3DataAnnoInfo[,16]==0 & (Obj3DataAnnoInfo[,11]>=0.2 | is.na(Obj3DataAnnoInfo[,11])),]
			Obj1NDRannoInObj1<-Obj1DataAnnoInfo[Obj1DataAnnoInfo[,16]==0 & Obj1DataAnnoInfo[,13]<pValue & (Obj1DataAnnoInfo[,11]>=0.2 | is.na(Obj1DataAnnoInfo[,11])),]
			Obj2NDRannoInObj2<-Obj2DataAnnoInfo[Obj2DataAnnoInfo[,16]==0 & Obj2DataAnnoInfo[,13]<pValue & (Obj2DataAnnoInfo[,11]>=0.2 | is.na(Obj2DataAnnoInfo[,11])),]
			Obj3NDRannoInObj3<-Obj3DataAnnoInfo[Obj3DataAnnoInfo[,16]==0 & Obj3DataAnnoInfo[,13]<pValue & (Obj3DataAnnoInfo[,11]>=0.2 | is.na(Obj3DataAnnoInfo[,11])),]
			
		}
		
	}
	
	
	
	Obj1NDRAnnoInfo<-array()
	Obj2NDRAnnoInfo<-array()
	Obj3NDRAnnoInfo<-array()
	
	for(i in c(1:22)){
		chrName=paste("chr",i,sep="")
		tmpObj1<-Obj1NDRanno[Obj1NDRanno[,1] %in% chrName,]
		tmpObj2<-Obj2NDRanno[Obj2NDRanno[,1] %in% chrName,]
		tmpObj3<-Obj3NDRanno[Obj3NDRanno[,1] %in% chrName,]
		
		
		Obj1LocData<-data.frame(chr= tmpObj1[,1], start= tmpObj1[,2], end= tmpObj1[,3])
		Obj2LocData<-data.frame(chr= tmpObj2[,1], start= tmpObj2[,2], end= tmpObj2[,3])
		Obj3LocData<-data.frame(chr= tmpObj3[,1], start= tmpObj3[,2], end= tmpObj3[,3])
		
		
		
		Obj1Obj2Overlapped<-as.matrix(regionOverlap(Obj1LocData, Obj2LocData))
		Obj1Obj3Overlapped<-as.matrix(regionOverlap(Obj1LocData, Obj3LocData))
		Obj2Obj3Overlapped<-as.matrix(regionOverlap(Obj2LocData, Obj3LocData))
		
		Obj1NDRAnnoInfo<-rbind(Obj1NDRAnnoInfo,cbind(tmpObj1, rowSums(Obj1Obj2Overlapped), rowSums(Obj1Obj3Overlapped)))
		Obj2NDRAnnoInfo<-rbind(Obj2NDRAnnoInfo,cbind(tmpObj2, rowSums(t(Obj1Obj2Overlapped)), rowSums(Obj2Obj3Overlapped)))
		Obj3NDRAnnoInfo<-rbind(Obj3NDRAnnoInfo,cbind(tmpObj3, rowSums(t(Obj1Obj3Overlapped)), rowSums(t(Obj2Obj3Overlapped))))
		#	print(dim(tmpObj3)[1])
		#	Obj2Obj3OverlappedT<-t(Obj2Obj3Overlapped)
		#	print(dim(Obj2Obj3OverlappedT[rowSums(Obj2Obj3OverlappedT)!=0,])[1])
		
	}
	Obj1NDRAnnoInfo<-Obj1NDRAnnoInfo[2:length(Obj1NDRAnnoInfo[,1]),]
	Obj2NDRAnnoInfo<-Obj2NDRAnnoInfo[2:length(Obj2NDRAnnoInfo[,1]),]
	Obj3NDRAnnoInfo<-Obj3NDRAnnoInfo[2:length(Obj3NDRAnnoInfo[,1]),]
	
	Obj1NDRAnnoInfo<-Obj1NDRAnnoInfo[!is.na(Obj1NDRAnnoInfo[,1]),]
	Obj2NDRAnnoInfo<-Obj2NDRAnnoInfo[!is.na(Obj2NDRAnnoInfo[,1]),]
	Obj3NDRAnnoInfo<-Obj3NDRAnnoInfo[!is.na(Obj3NDRAnnoInfo[,1]),]
	
	NumObj1Obj2NDRoverlappedAnno<-dim(Obj1NDRAnnoInfo[Obj1NDRAnnoInfo[,17]!=0,])
	NumObj1Obj3NDRoverlappedAnno<-dim(Obj3NDRAnnoInfo[Obj3NDRAnnoInfo[,17]!=0,])
	NumObj2Obj3NDRoverlappedAnno<-dim(Obj3NDRAnnoInfo[Obj3NDRAnnoInfo[,18]!=0,])
#	NumObj3Obj2NDRoverlappedAnno<-dim(Obj3NDRAnnoInfo[Obj3NDRAnnoInfo[,18]!=0,])
	NumNDRoverlapped3Anno<-dim(Obj3NDRAnnoInfo[Obj3NDRAnnoInfo[,17]!=0 & Obj3NDRAnnoInfo[,18]!=0,])
	
	
	Obj1LocNDRanno<-data.frame(chr= Obj1NDRanno$chr, start= Obj1NDRanno$start, end= Obj1NDRanno$end)
	Obj2LocNDRanno<-data.frame(chr= Obj2NDRanno$chr, start= Obj2NDRanno$start, end= Obj2NDRanno$end)
	Obj3LocNDRanno<-data.frame(chr= Obj3NDRanno[,1], start= Obj3NDRanno[,2], end= Obj3NDRanno[,3])
	Obj1Obj2NDRoverlappedAnno<-as.matrix(regionOverlap(Obj1LocNDRanno, Obj2LocNDRanno))
#	NumObj1Obj2NDRoverlappedAnno<-dim(Obj1Obj2NDRoverlappedAnno[rowSums(Obj1Obj2NDRoverlappedAnno)!=0,])
	Obj1Obj3NDRoverlappedAnno<-as.matrix(regionOverlap(Obj1LocNDRanno, Obj3LocNDRanno))
#	NumObj1Obj3NDRoverlappedAnno<-dim(Obj1Obj3NDRoverlappedAnno[rowSums(Obj1Obj3NDRoverlappedAnno)!=0,])
	Obj2Obj3NDRoverlappedAnno<-as.matrix(regionOverlap(Obj2LocNDRanno, Obj3LocNDRanno))
#	NumObj2Obj3NDRoverlappedAnno<-dim(Obj2Obj3NDRoverlappedAnno[rowSums(Obj2Obj3NDRoverlappedAnno)!=0,])
	overlapped3Anno<-cbind(rowSums(Obj1Obj2NDRoverlappedAnno),rowSums(Obj1Obj3NDRoverlappedAnno))
#	NumNDRoverlapped3Anno <- dim(overlapped3Anno[overlapped3Anno[,1]!=0 & overlapped3Anno[,2]!=0,])
}


cat("Total callable window of Obj1:")
cat(dim(Obj1)[1])
cat("\n")
cat("Total callable window of Obj2:")
cat(dim(Obj2)[1])
cat("\n")
cat("Total callable window of Obj3:")
cat(dim(Obj3)[1])
cat("\n")
cat("Total NDR of Obj1:")
cat(dim(Obj1NDRInObj1)[1])
cat("\n")
cat("Total NDR of Obj2:")
cat(dim(Obj2NDRInObj2)[1])
cat("\n")

cat("NDR percentage in callable window of Obj1:")
cat(dim(Obj1NDRInObj1)[1]/dim(Obj1)[1])
cat("\n")
cat("NDR percentage in callable window of Obj2:")
cat(dim(Obj2NDRInObj2)[1]/dim(Obj2)[1])
cat("\n")

cat("Number of shared callable window in annotation file:")
cat(dim(sharedWindow)[1])
cat("\n")
cat("NDR of Obj1 in shared callable window:")
cat(dim(Obj1NDR)[1])
cat("\n")
cat("NDR of Obj2 in shared callable window:")
cat(dim(Obj2NDR)[1])
cat("\n")
cat("number of Obj3 in shared callable window:")
cat(dim(Obj3NDR)[1])
cat("\n")

cat("NDR overlapped between Obj1 and Obj2:")
cat(NumObj1Obj2NDRoverlapped[1])
cat("\n")
cat("NDR overlapped between Obj1 and Obj3:")
cat(NumObj1Obj3NDRoverlapped[1])
cat("\n")
cat("NDR overlapped between Obj2 and Obj3:")
cat(NumObj2Obj3NDRoverlapped[1])
cat("\n")
cat("NDR overlapped among Obj1, Obj2 and Obj3:")
cat(NumNDRoverlapped3[1])
cat("\n")
cat("percentage of NDR in Obj1 overlapped between Obj1 and Obj2:")
cat(NumObj1Obj2NDRoverlapped[1]/dim(Obj1NDR)[1])
cat("\n")
cat("percentage of NDR in Obj2 overlapped between Obj1 and Obj2:")
cat(NumObj1Obj2NDRoverlapped[1]/dim(Obj2NDR)[1])
cat("\n")
cat("percentage of NDR in Obj1 overlapped between Obj1 and Obj3:")
cat(NumObj1Obj3NDRoverlapped[1]/dim(Obj1NDR)[1])
cat("\n")
cat("percentage of NDR in Obj3 overlapped between Obj1 and Obj3:")
cat(NumObj1Obj3NDRoverlapped[1]/dim(Obj3NDR)[1])
cat("\n")

if(!is.null(anno)){
	cat("Total callable window of Obj1 in annotation file:")
	cat(dim(Obj1Anno)[1])
	cat("\n")
	cat("Total callable window of Obj2 in annotation file:")
	cat(dim(Obj2Anno)[1])
	cat("\n")
	cat("Total callable window of Obj3 in annotation file:")
	cat(dim(Obj3Anno)[1])
	cat("\n")
	cat("Total NDR of Obj1 in annotation file:")
	cat(dim(Obj1NDRannoInObj1)[1])
	cat("\n")
	cat("Total NDR of Obj2 in annotation file:")
	cat(dim(Obj2NDRannoInObj2)[1])
	cat("\n")
	cat("Total NDR of Obj3 in annotation file:")
	cat(dim(Obj3NDRannoInObj3)[1])
	cat("\n")
	cat("NDR percentage in callable window of Obj1 in annotation file:")
	cat(dim(Obj1NDRannoInObj1)[1]/dim(Obj1Anno)[1])
	cat("\n")
	cat("NDR percentage in callable window of Obj2 in annotation file:")
	cat(dim(Obj2NDRannoInObj2)[1]/dim(Obj2Anno)[1])
	cat("\n")
	cat("NDR percentage in callable window of Obj3 in annotation file:")
	cat(dim(Obj3NDRannoInObj3)[1]/dim(Obj3Anno)[1])
	cat("\n")
	cat("Number of shared callable window in annotation file:")
	cat(dim(sharedWindAnno)[1])
	cat("\n")
	cat("NDR of Obj1 in shared callable window in annotation file:")
	cat(dim(Obj1NDRanno)[1])
	cat("\n")
	cat("NDR of Obj2 in shared callable window in annotation file:")
	cat(dim(Obj2NDRanno)[1])
	cat("\n")
	cat("NDR of Obj3 in shared callable window in annotation file:")
	cat(dim(Obj3NDRanno)[1])
	cat("\n")
	cat("NDR overlapped between Obj1 and Obj2 in annotation file:")
	cat(NumObj1Obj2NDRoverlappedAnno[1])
	cat("\n")
	cat("NDR overlapped between Obj1 and Obj3 in annotation file:")
	cat(NumObj1Obj3NDRoverlappedAnno[1])
	cat("\n")
	cat("NDR overlapped between Obj2 and Obj3 in annotation file:")
	cat(NumObj2Obj3NDRoverlappedAnno[1])
	cat("\n")
	cat("NDR overlapped among Obj1, Obj2 and Obj3 in annotation file:")
	cat(NumNDRoverlapped3Anno[1])
	cat("\n")
	cat("percentage of NDR in Obj1  overlapped between Obj1 and Obj2 in annotation file:")
	cat(NumObj1Obj2NDRoverlappedAnno[1]/dim(Obj1NDRanno)[1])
	cat("\n")
	cat("percentage of NDR in Obj2  overlapped between Obj1 and Obj2 in annotation file:")
	cat(NumObj1Obj2NDRoverlappedAnno[1]/dim(Obj2NDRanno)[1])
	cat("\n")
	cat("percentage of NDR in Obj1  overlapped between Obj1 and Obj3 in annotation file:")
	cat(NumObj1Obj3NDRoverlappedAnno[1]/dim(Obj1NDRanno)[1])
	cat("\n")
	cat("percentage of NDR in Obj3  overlapped between Obj1 and Obj3 in annotation file:")
	cat(NumObj1Obj3NDRoverlappedAnno[1]/dim(Obj3NDRanno)[1])
	cat("\n")
}

if(outputBed == 1){
	library(ChIPpeakAnno)
	data(TSS.human.GRCh37)
	Obj1NDRoverlappedOutputAnno<-cbind(Obj1NDR,rowSums(Obj1Obj2NDRoverlapped),rowSums(Obj1Obj3NDRoverlapped))
	Obj2NDRoverlappedOutputAnno<-cbind(Obj2NDR,rowSums(t(Obj1Obj2NDRoverlapped)),rowSums(Obj2Obj3NDRoverlapped))
	Obj3NDRoverlappedOutputAnno<-cbind(Obj3NDR,rowSums(t(Obj1Obj3NDRoverlapped)),rowSums(t(Obj2Obj3NDRoverlapped)))
	
	
	Obj1_Obj2_only<-Obj1NDRoverlappedOutputAnno[Obj1NDRoverlappedOutputAnno[,16]!=0 & Obj1NDRoverlappedOutputAnno[,17]==0,]
	Obj1_Obj3_only<-Obj1NDRoverlappedOutputAnno[Obj1NDRoverlappedOutputAnno[,16]==0 & Obj1NDRoverlappedOutputAnno[,17]!=0,]
	Obj1_Obj2<-Obj1NDRoverlappedOutputAnno[Obj1NDRoverlappedOutputAnno[,16]!=0,]
	Obj1_Obj3<-Obj1NDRoverlappedOutputAnno[Obj1NDRoverlappedOutputAnno[,17]!=0,]
	Obj1_Obj2_no<-Obj1NDRoverlappedOutputAnno[Obj1NDRoverlappedOutputAnno[,16]==0,]
	Obj1_Obj3_no<-Obj1NDRoverlappedOutputAnno[Obj1NDRoverlappedOutputAnno[,17]==0,]
	Obj1_only<-Obj1NDRoverlappedOutputAnno[Obj1NDRoverlappedOutputAnno[,16]==0 & Obj1NDRoverlappedOutputAnno[,17]==0,]
	Obj2_only<-Obj2NDRoverlappedOutputAnno[Obj2NDRoverlappedOutputAnno[,16]==0 & Obj2NDRoverlappedOutputAnno[,17]==0,]
	Obj3_only<-Obj3NDRoverlappedOutputAnno[Obj3NDRoverlappedOutputAnno[,16]==0 & Obj3NDRoverlappedOutputAnno[,17]==0,]
	Obj1_Obj2_Obj3<-Obj1NDRoverlappedOutputAnno[Obj1NDRoverlappedOutputAnno[,16]!=0 & Obj1NDRoverlappedOutputAnno[,17]!=0,]
	
	write.table(Obj1_Obj2_only[,1:3],"Obj1_Obj2_only.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(Obj1_Obj3_only[,1:3],"Obj1_Obj3_only.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(Obj1_Obj2[,1:3],"Obj1_Obj2.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(Obj1_Obj3[,1:3],"Obj1_Obj3.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(Obj1_Obj2_no[,1:3],"Obj1_Obj2_no.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(Obj1_Obj3_no[,1:3],"Obj1_Obj3_no.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(Obj1_only[,1:3],"Obj1_only.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(Obj2_only[,1:3],"Obj2_only.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(Obj3_only[,1:3],"Obj3_only.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(Obj1_Obj2_Obj3[,1:3],"Obj1_Obj2_Obj3.bed",quote=F,sep="\t",row.names=F,col.names=F)
	
	rangeObj1_Obj2_only<-BED2RangedData(Obj1_Obj2_only[,1:3],header=FALSE)
	rangeObj1_Obj3_only<-BED2RangedData(Obj1_Obj3_only[,1:3],header=FALSE)
	rangeObj1_Obj2<-BED2RangedData(Obj1_Obj2[,1:3],header=FALSE)
	rangeObj1_Obj3<-BED2RangedData(Obj1_Obj3[,1:3],header=FALSE)
	rangeObj1_Obj2_no<-BED2RangedData(Obj1_Obj2_no[,1:3],header=FALSE)
	rangeObj1_Obj3_no<-BED2RangedData(Obj1_Obj3_no[,1:3],header=FALSE)
	rangeObj1_only<-BED2RangedData(Obj1_only[,1:3],header=FALSE)
	rangeObj2_only<-BED2RangedData(Obj2_only[,1:3],header=FALSE)
	rangeObj3_only<-BED2RangedData(Obj3_only[,1:3],header=FALSE)
	rangeObj1_Obj2_Obj3<-BED2RangedData(Obj1_Obj2_Obj3[,1:3],header=FALSE)
	
	annObj1_Obj2_only<-annotatePeakInBatch(rangeObj1_Obj2_only,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
	annObj1_Obj3_only<-annotatePeakInBatch(rangeObj1_Obj3_only,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
	annObj1_Obj2<-annotatePeakInBatch(rangeObj1_Obj2,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
	annObj1_Obj3<-annotatePeakInBatch(rangeObj1_Obj3,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
	annObj1_Obj2_no<-annotatePeakInBatch(rangeObj1_Obj2_no,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
	annObj1_Obj3_no<-annotatePeakInBatch(rangeObj1_Obj3_no,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
	annObj1_only<-annotatePeakInBatch(rangeObj1_only,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
	annObj2_only<-annotatePeakInBatch(rangeObj2_only,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
	annObj3_only<-annotatePeakInBatch(rangeObj3_only,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
	annObj1_Obj2_Obj3<-annotatePeakInBatch(rangeObj1_Obj2_Obj3,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
	
	fullAnnObj1_Obj2_only<-addGeneIDs(annObj1_Obj2_only,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
	fullAnnObj1_Obj3_only<-addGeneIDs(annObj1_Obj3_only,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
	fullAnnObj1_Obj2<-addGeneIDs(annObj1_Obj2,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
	fullAnnObj1_Obj3<-addGeneIDs(annObj1_Obj3,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
	fullAnnObj1_Obj2_no<-addGeneIDs(annObj1_Obj2_no,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
	fullAnnObj1_Obj3_no<-addGeneIDs(annObj1_Obj3_no,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
	fullAnnObj1_only<-addGeneIDs(annObj1_only,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
	fullAnnObj2_only<-addGeneIDs(annObj2_only,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
	fullAnnObj3_only<-addGeneIDs(annObj3_only,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
	fullAnnObj1_Obj2_Obj3<-addGeneIDs(annObj1_Obj2_Obj3,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
	
	write.table(cbind(Obj1_Obj2_only[,1:3],annObj1_Obj2_only$feature),"Obj1_Obj2_only.annotate.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(cbind(Obj1_Obj3_only[,1:3],annObj1_Obj3_only$feature),"Obj1_Obj3_only.annotate.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(cbind(Obj1_Obj2[,1:3],annObj1_Obj2$feature),"Obj1_Obj2.annotate.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(cbind(Obj1_Obj3[,1:3],annObj1_Obj3$feature),"Obj1_Obj3.annotate.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(cbind(Obj1_Obj2_no[,1:3],annObj1_Obj2_no$feature),"Obj1_Obj2_no.annotate.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(cbind(Obj1_Obj3_no[,1:3],annObj1_Obj3_no$feature),"Obj1_Obj3_no.annotate.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(cbind(Obj1_only[,1:3],annObj1_only$feature),"Obj1_only.annotate.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(cbind(Obj2_only[,1:3],annObj2_only$feature),"Obj2_only.annotate.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(cbind(Obj3_only[,1:3],annObj3_only$feature),"Obj3_only.annotate.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(cbind(Obj1_Obj2_Obj3[,1:3],annObj1_Obj2_Obj3$feature),"Obj1_Obj2_Obj3.annotate.bed",quote=F,sep="\t",row.names=F,col.names=F)
	
	
	write.table(cbind(Obj1_Obj2_only[,1:3],fullAnnObj1_Obj2_only$feature,fullAnnObj1_Obj2_only$symbol,fullAnnObj1_Obj2_only$genename),"Obj1_Obj2_only.annotate.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(cbind(Obj1_Obj3_only[,1:3],fullAnnObj1_Obj3_only$feature,fullAnnObj1_Obj3_only$symbol,fullAnnObj1_Obj3_only$genename),"Obj1_Obj3_only.annotate.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(cbind(Obj1_Obj2[,1:3],fullAnnObj1_Obj2$feature,fullAnnObj1_Obj2$symbol,fullAnnObj1_Obj2$genename),"Obj1_Obj2.annotate.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(cbind(Obj1_Obj3[,1:3],fullAnnObj1_Obj3$feature,fullAnnObj1_Obj3$symbol,fullAnnObj1_Obj3$genename),"Obj1_Obj3.annotate.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(cbind(Obj1_Obj2_no[,1:3],fullAnnObj1_Obj2_no$feature,fullAnnObj1_Obj2_no$symbol,fullAnnObj1_Obj2_no$genename),"Obj1_Obj2_no.annotate.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(cbind(Obj1_Obj3_no[,1:3],fullAnnObj1_Obj3_no$feature,fullAnnObj1_Obj3_no$symbol,fullAnnObj1_Obj3_no$genename),"Obj1_Obj3_no.annotate.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(cbind(Obj1_only[,1:3],fullAnnObj1_only$feature,fullAnnObj1_only$symbol,fullAnnObj1_only$genename),"Obj1_only.annotate.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(cbind(Obj2_only[,1:3],fullAnnObj2_only$feature,fullAnnObj2_only$symbol,fullAnnObj2_only$genename),"Obj2_only.annotate.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(cbind(Obj3_only[,1:3],fullAnnObj3_only$feature,fullAnnObj3_only$symbol,fullAnnObj3_only$genename),"Obj3_only.annotate.bed",quote=F,sep="\t",row.names=F,col.names=F)
	write.table(cbind(Obj1_Obj2_Obj3[,1:3],fullAnnObj1_Obj2_Obj3$feature,fullAnnObj1_Obj2_Obj3$symbol,fullAnnObj1_Obj2_Obj3$genename),"Obj1_Obj2_Obj3.annotate.bed",quote=F,sep="\t",row.names=F,col.names=F)
	
	if(!is.null(anno)){
		
		
		Obj1NDRoverlappedOutputAnno<-cbind(Obj1NDRanno,rowSums(Obj1Obj2NDRoverlappedAnno),rowSums(Obj1Obj3NDRoverlappedAnno))
		Obj2NDRoverlappedOutputAnno<-cbind(Obj2NDRanno,rowSums(t(Obj1Obj2NDRoverlappedAnno)),rowSums(Obj2Obj3NDRoverlappedAnno))
		Obj3NDRoverlappedOutputAnno<-cbind(Obj3NDRanno,rowSums(t(Obj2Obj3NDRoverlappedAnno)),rowSums(t(Obj1Obj3NDRoverlappedAnno)))
#CGI TSS
		Obj1_Obj2_only<-Obj1NDRoverlappedOutputAnno[Obj1NDRoverlappedOutputAnno[,17]!=0 & Obj1NDRoverlappedOutputAnno[,18]==0,]
		Obj1_Obj3_only<-Obj1NDRoverlappedOutputAnno[Obj1NDRoverlappedOutputAnno[,17]==0 & Obj1NDRoverlappedOutputAnno[,18]!=0,]
		Obj1_Obj2<-Obj1NDRoverlappedOutputAnno[Obj1NDRoverlappedOutputAnno[,17]!=0,]
		Obj1_Obj3<-Obj1NDRoverlappedOutputAnno[Obj1NDRoverlappedOutputAnno[,18]!=0,]
		Obj1_Obj2_no<-Obj1NDRoverlappedOutputAnno[Obj1NDRoverlappedOutputAnno[,17]==0,]
		Obj1_Obj3_no<-Obj1NDRoverlappedOutputAnno[Obj1NDRoverlappedOutputAnno[,18]==0,]
		Obj1_only<-Obj1NDRoverlappedOutputAnno[Obj1NDRoverlappedOutputAnno[,17]==0 & Obj1NDRoverlappedOutputAnno[,18]==0,]
		Obj2_only<-Obj2NDRoverlappedOutputAnno[Obj2NDRoverlappedOutputAnno[,17]==0 & Obj2NDRoverlappedOutputAnno[,18]==0,]
		Obj3_only<-Obj3NDRoverlappedOutputAnno[Obj3NDRoverlappedOutputAnno[,17]==0 & Obj3NDRoverlappedOutputAnno[,18]==0,]
		Obj1_Obj2_Obj3<-Obj1NDRoverlappedOutputAnno[Obj1NDRoverlappedOutputAnno[,17]!=0 & Obj1NDRoverlappedOutputAnno[,18]!=0,]
		write.table(Obj1_Obj2_only[,1:3],"Obj1_Obj2_only.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(Obj1_Obj3_only[,1:3],"Obj1_Obj3_only.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(Obj1_Obj2[,1:3],"Obj1_Obj2.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(Obj1_Obj3[,1:3],"Obj1_Obj3.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(Obj1_Obj2_no[,1:3],"Obj1_Obj2_no.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(Obj1_Obj3_no[,1:3],"Obj1_Obj3_no.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(Obj1_only[,1:3],"Obj1_only.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(Obj2_only[,1:3],"Obj2_only.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(Obj3_only[,1:3],"Obj3_only.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(Obj1_Obj2_Obj3[,1:3],"Obj1_Obj2_Obj3.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		
		rangeObj1_Obj2_only<-BED2RangedData(Obj1_Obj2_only[,1:3],header=FALSE)
		rangeObj1_Obj3_only<-BED2RangedData(Obj1_Obj3_only[,1:3],header=FALSE)
		rangeObj1_Obj2<-BED2RangedData(Obj1_Obj2[,1:3],header=FALSE)
		rangeObj1_Obj3<-BED2RangedData(Obj1_Obj3[,1:3],header=FALSE)
		rangeObj1_Obj2_no<-BED2RangedData(Obj1_Obj2_no[,1:3],header=FALSE)
		rangeObj1_Obj3_no<-BED2RangedData(Obj1_Obj3_no[,1:3],header=FALSE)
		rangeObj1_only<-BED2RangedData(Obj1_only[,1:3],header=FALSE)
		rangeObj2_only<-BED2RangedData(Obj2_only[,1:3],header=FALSE)
		rangeObj3_only<-BED2RangedData(Obj3_only[,1:3],header=FALSE)
		rangeObj1_Obj2_Obj3<-BED2RangedData(Obj1_Obj2_Obj3[,1:3],header=FALSE)
		
		annObj1_Obj2_only<-annotatePeakInBatch(rangeObj1_Obj2_only,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
		annObj1_Obj3_only<-annotatePeakInBatch(rangeObj1_Obj3_only,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
		annObj1_Obj2<-annotatePeakInBatch(rangeObj1_Obj2,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
		annObj1_Obj3<-annotatePeakInBatch(rangeObj1_Obj3,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
		annObj1_Obj2_no<-annotatePeakInBatch(rangeObj1_Obj2_no,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
		annObj1_Obj3_no<-annotatePeakInBatch(rangeObj1_Obj3_no,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
		annObj1_only<-annotatePeakInBatch(rangeObj1_only,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
		annObj2_only<-annotatePeakInBatch(rangeObj2_only,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
		annObj3_only<-annotatePeakInBatch(rangeObj3_only,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
		annObj1_Obj2_Obj3<-annotatePeakInBatch(rangeObj1_Obj2_Obj3,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
		
		fullAnnObj1_Obj2_only<-addGeneIDs(annObj1_Obj2_only,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
		fullAnnObj1_Obj3_only<-addGeneIDs(annObj1_Obj3_only,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
		fullAnnObj1_Obj2<-addGeneIDs(annObj1_Obj2,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
		fullAnnObj1_Obj3<-addGeneIDs(annObj1_Obj3,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
		fullAnnObj1_Obj2_no<-addGeneIDs(annObj1_Obj2_no,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
		fullAnnObj1_Obj3_no<-addGeneIDs(annObj1_Obj3_no,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
		fullAnnObj1_only<-addGeneIDs(annObj1_only,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
		fullAnnObj2_only<-addGeneIDs(annObj2_only,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
		fullAnnObj3_only<-addGeneIDs(annObj3_only,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
		fullAnnObj1_Obj2_Obj3<-addGeneIDs(annObj1_Obj2_Obj3,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
		
		write.table(cbind(Obj1_Obj2_only[,1:3],annObj1_Obj2_only$feature),"Obj1_Obj2_only.annotate.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(cbind(Obj1_Obj3_only[,1:3],annObj1_Obj3_only$feature),"Obj1_Obj3_only.annotate.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(cbind(Obj1_Obj2[,1:3],annObj1_Obj2$feature),"Obj1_Obj2.annotate.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(cbind(Obj1_Obj3[,1:3],annObj1_Obj3$feature),"Obj1_Obj3.annotate.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(cbind(Obj1_Obj2_no[,1:3],annObj1_Obj2_no$feature),"Obj1_Obj2_no.annotate.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(cbind(Obj1_Obj3_no[,1:3],annObj1_Obj3_no$feature),"Obj1_Obj3_no.annotate.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(cbind(Obj1_only[,1:3],annObj1_only$feature),"Obj1_only.annotate.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(cbind(Obj2_only[,1:3],annObj2_only$feature),"Obj2_only.annotate.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(cbind(Obj3_only[,1:3],annObj3_only$feature),"Obj3_only.annotate.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(cbind(Obj1_Obj2_Obj3[,1:3],annObj1_Obj2_Obj3$feature),"Obj1_Obj2_Obj3.annotate.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		
		
		write.table(cbind(Obj1_Obj2_only[,1:3],fullAnnObj1_Obj2_only$feature,fullAnnObj1_Obj2_only$symbol,fullAnnObj1_Obj2_only$genename),"Obj1_Obj2_only.annotate.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(cbind(Obj1_Obj3_only[,1:3],fullAnnObj1_Obj3_only$feature,fullAnnObj1_Obj3_only$symbol,fullAnnObj1_Obj3_only$genename),"Obj1_Obj3_only.annotate.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(cbind(Obj1_Obj2[,1:3],fullAnnObj1_Obj2$feature,fullAnnObj1_Obj2$symbol,fullAnnObj1_Obj2$genename),"Obj1_Obj2.annotate.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(cbind(Obj1_Obj3[,1:3],fullAnnObj1_Obj3$feature,fullAnnObj1_Obj3$symbol,fullAnnObj1_Obj3$genename),"Obj1_Obj3.annotate.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(cbind(Obj1_Obj2_no[,1:3],fullAnnObj1_Obj2_no$feature,fullAnnObj1_Obj2_no$symbol,fullAnnObj1_Obj2_no$genename),"Obj1_Obj2_no.annotate.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(cbind(Obj1_Obj3_no[,1:3],fullAnnObj1_Obj3_no$feature,fullAnnObj1_Obj3_no$symbol,fullAnnObj1_Obj3_no$genename),"Obj1_Obj3_no.annotate.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(cbind(Obj1_only[,1:3],fullAnnObj1_only$feature,fullAnnObj1_only$symbol,fullAnnObj1_only$genename),"Obj1_only.annotate.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(cbind(Obj2_only[,1:3],fullAnnObj2_only$feature,fullAnnObj2_only$symbol,fullAnnObj2_only$genename),"Obj2_only.annotate.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(cbind(Obj3_only[,1:3],fullAnnObj3_only$feature,fullAnnObj3_only$symbol,fullAnnObj3_only$genename),"Obj3_only.annotate.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		write.table(cbind(Obj1_Obj2_Obj3[,1:3],fullAnnObj1_Obj2_Obj3$feature,fullAnnObj1_Obj2_Obj3$symbol,fullAnnObj1_Obj2_Obj3$genename),"Obj1_Obj2_Obj3.annotate.CGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		if(overlap==0){
			#Non_CGI TSS
			Obj1_Obj2_only<-Obj1NDRoverlappedOutputAnno[Obj1NDRoverlappedOutputAnno[,17]!=0 & Obj1NDRoverlappedOutputAnno[,18]==0,]
			Obj1_Obj3_only<-Obj1NDRoverlappedOutputAnno[Obj1NDRoverlappedOutputAnno[,17]==0 & Obj1NDRoverlappedOutputAnno[,18]!=0,]
			Obj1_Obj2<-Obj1NDRoverlappedOutputAnno[Obj1NDRoverlappedOutputAnno[,17]!=0,]
			Obj1_Obj3<-Obj1NDRoverlappedOutputAnno[Obj1NDRoverlappedOutputAnno[,18]!=0,]
			Obj1_Obj2_no<-Obj1NDRoverlappedOutputAnno[Obj1NDRoverlappedOutputAnno[,17]==0,]
			Obj1_Obj3_no<-Obj1NDRoverlappedOutputAnno[Obj1NDRoverlappedOutputAnno[,18]==0,]
			Obj1_only<-Obj1NDRoverlappedOutputAnno[Obj1NDRoverlappedOutputAnno[,17]==0 & Obj1NDRoverlappedOutputAnno[,18]==0,]
			Obj2_only<-Obj2NDRoverlappedOutputAnno[Obj2NDRoverlappedOutputAnno[,17]==0 & Obj2NDRoverlappedOutputAnno[,18]==0,]
			Obj3_only<-Obj3NDRoverlappedOutputAnno[Obj3NDRoverlappedOutputAnno[,17]==0 & Obj3NDRoverlappedOutputAnno[,18]==0,]
			Obj1_Obj2_Obj3<-Obj1NDRoverlappedOutputAnno[Obj1NDRoverlappedOutputAnno[,17]!=0 & Obj1NDRoverlappedOutputAnno[,18]!=0,]
			write.table(Obj1_Obj2_only[,1:3],"Obj1_Obj2_only.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(Obj1_Obj3_only[,1:3],"Obj1_Obj3_only.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(Obj1_Obj2[,1:3],"Obj1_Obj2.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(Obj1_Obj3[,1:3],"Obj1_Obj3.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(Obj1_Obj2_no[,1:3],"Obj1_Obj2_no.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(Obj1_Obj3_no[,1:3],"Obj1_Obj3_no.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(Obj1_only[,1:3],"Obj1_only.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(Obj2_only[,1:3],"Obj2_only.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(Obj3_only[,1:3],"Obj3_only.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(Obj1_Obj2_Obj3[,1:3],"Obj1_Obj2_Obj3.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			
			rangeObj1_Obj2_only<-BED2RangedData(Obj1_Obj2_only[,1:3],header=FALSE)
			rangeObj1_Obj3_only<-BED2RangedData(Obj1_Obj3_only[,1:3],header=FALSE)
			rangeObj1_Obj2<-BED2RangedData(Obj1_Obj2[,1:3],header=FALSE)
			rangeObj1_Obj3<-BED2RangedData(Obj1_Obj3[,1:3],header=FALSE)
			rangeObj1_Obj2_no<-BED2RangedData(Obj1_Obj2_no[,1:3],header=FALSE)
			rangeObj1_Obj3_no<-BED2RangedData(Obj1_Obj3_no[,1:3],header=FALSE)
			rangeObj1_only<-BED2RangedData(Obj1_only[,1:3],header=FALSE)
			rangeObj2_only<-BED2RangedData(Obj2_only[,1:3],header=FALSE)
			rangeObj3_only<-BED2RangedData(Obj3_only[,1:3],header=FALSE)
			rangeObj1_Obj2_Obj3<-BED2RangedData(Obj1_Obj2_Obj3[,1:3],header=FALSE)
			
			annObj1_Obj2_only<-annotatePeakInBatch(rangeObj1_Obj2_only,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
			annObj1_Obj3_only<-annotatePeakInBatch(rangeObj1_Obj3_only,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
			annObj1_Obj2<-annotatePeakInBatch(rangeObj1_Obj2,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
			annObj1_Obj3<-annotatePeakInBatch(rangeObj1_Obj3,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
			annObj1_Obj2_no<-annotatePeakInBatch(rangeObj1_Obj2_no,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
			annObj1_Obj3_no<-annotatePeakInBatch(rangeObj1_Obj3_no,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
			annObj1_only<-annotatePeakInBatch(rangeObj1_only,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
			annObj2_only<-annotatePeakInBatch(rangeObj2_only,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
			annObj3_only<-annotatePeakInBatch(rangeObj3_only,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
			annObj1_Obj2_Obj3<-annotatePeakInBatch(rangeObj1_Obj2_Obj3,AnnotationData=TSS.human.GRCh37, featureType=c("TSS"))
			
			fullAnnObj1_Obj2_only<-addGeneIDs(annObj1_Obj2_only,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
			fullAnnObj1_Obj3_only<-addGeneIDs(annObj1_Obj3_only,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
			fullAnnObj1_Obj2<-addGeneIDs(annObj1_Obj2,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
			fullAnnObj1_Obj3<-addGeneIDs(annObj1_Obj3,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
			fullAnnObj1_Obj2_no<-addGeneIDs(annObj1_Obj2_no,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
			fullAnnObj1_Obj3_no<-addGeneIDs(annObj1_Obj3_no,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
			fullAnnObj1_only<-addGeneIDs(annObj1_only,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
			fullAnnObj2_only<-addGeneIDs(annObj2_only,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
			fullAnnObj3_only<-addGeneIDs(annObj3_only,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
			fullAnnObj1_Obj2_Obj3<-addGeneIDs(annObj1_Obj2_Obj3,"org.Hs.eg.db",IDs2Add=c("symbol","genename"),feature_id_type="ensembl_gene_id")
			
			write.table(cbind(Obj1_Obj2_only[,1:3],annObj1_Obj2_only$feature),"Obj1_Obj2_only.annotate.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(cbind(Obj1_Obj3_only[,1:3],annObj1_Obj3_only$feature),"Obj1_Obj3_only.annotate.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(cbind(Obj1_Obj2[,1:3],annObj1_Obj2$feature),"Obj1_Obj2.annotate.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(cbind(Obj1_Obj3[,1:3],annObj1_Obj3$feature),"Obj1_Obj3.annotate.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(cbind(Obj1_Obj2_no[,1:3],annObj1_Obj2_no$feature),"Obj1_Obj2_no.annotate.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(cbind(Obj1_Obj3_no[,1:3],annObj1_Obj3_no$feature),"Obj1_Obj3_no.annotate.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(cbind(Obj1_only[,1:3],annObj1_only$feature),"Obj1_only.annotate.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(cbind(Obj2_only[,1:3],annObj2_only$feature),"Obj2_only.annotate.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(cbind(Obj3_only[,1:3],annObj3_only$feature),"Obj3_only.annotate.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(cbind(Obj1_Obj2_Obj3[,1:3],annObj1_Obj2_Obj3$feature),"Obj1_Obj2_Obj3.annotate.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			
			
			write.table(cbind(Obj1_Obj2_only[,1:3],fullAnnObj1_Obj2_only$feature,fullAnnObj1_Obj2_only$symbol,fullAnnObj1_Obj2_only$genename),"Obj1_Obj2_only.annotate.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(cbind(Obj1_Obj3_only[,1:3],fullAnnObj1_Obj3_only$feature,fullAnnObj1_Obj3_only$symbol,fullAnnObj1_Obj3_only$genename),"Obj1_Obj3_only.annotate.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(cbind(Obj1_Obj2[,1:3],fullAnnObj1_Obj2$feature,fullAnnObj1_Obj2$symbol,fullAnnObj1_Obj2$genename),"Obj1_Obj2.annotate.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(cbind(Obj1_Obj3[,1:3],fullAnnObj1_Obj3$feature,fullAnnObj1_Obj3$symbol,fullAnnObj1_Obj3$genename),"Obj1_Obj3.annotate.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(cbind(Obj1_Obj2_no[,1:3],fullAnnObj1_Obj2_no$feature,fullAnnObj1_Obj2_no$symbol,fullAnnObj1_Obj2_no$genename),"Obj1_Obj2_no.annotate.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(cbind(Obj1_Obj3_no[,1:3],fullAnnObj1_Obj3_no$feature,fullAnnObj1_Obj3_no$symbol,fullAnnObj1_Obj3_no$genename),"Obj1_Obj3_no.annotate.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(cbind(Obj1_only[,1:3],fullAnnObj1_only$feature,fullAnnObj1_only$symbol,fullAnnObj1_only$genename),"Obj1_only.annotate.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(cbind(Obj2_only[,1:3],fullAnnObj2_only$feature,fullAnnObj2_only$symbol,fullAnnObj2_only$genename),"Obj2_only.annotate.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(cbind(Obj3_only[,1:3],fullAnnObj3_only$feature,fullAnnObj3_only$symbol,fullAnnObj3_only$genename),"Obj3_only.annotate.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
			write.table(cbind(Obj1_Obj2_Obj3[,1:3],fullAnnObj1_Obj2_Obj3$feature,fullAnnObj1_Obj2_Obj3$symbol,fullAnnObj1_Obj2_Obj3$genename),"Obj1_Obj2_Obj3.annotate.nonCGI.bed",quote=F,sep="\t",row.names=F,col.names=F)
		}
		
		
		
	}
	
}


